#include "thaisenPowerControl.h"

#include "thaisenChargModule.h"

#define DBG_TAG "thaisenPowerControl"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define THAISEN_TERMINAL_ADDRESS_TEMINAL                   0x2F  //终端充电主控模块源地址 0x30 ～ 0x4E,设置屏幕A为1,0x2F+1=0x30,符合使用逻辑

#define THAISEN_GUN_A     0
#define THAISEN_GUN_B     1
#define THAISEN_GUN_NUM   2

#define THAISEN_POWER_CONTROL_CHARGE_RELAY                 0x40                /* 控制字节：充电回路主接触器状态 */
#define THAISEN_POWERCONTROL_POWER_RELAY                   0x20                /* 控制字节：功率分配回路接触器状态 */
#define THAISEN_POWERCONTROL_VOLT_RANGE                    0x10                /* 控制字节：电压输出范围选择 */
#define THAISEN_POWERCONTROL_STOP_CHARGE                   0x06                /* 控制字节停止充电 （充电模块退出运行，可给其他充电端口使用） */
#define THAISEN_POWERCONTROL_PARA_MODIFY                   0x05                /* 控制字节：参数修改 */
#define THAISEN_POWERCONTROL_SOFT_START                    0x03                /* 控制字节：软启动 */
#define THAISEN_POWERCONTROL_OPEN_FAST                     0x01                /* 控制字节：快速开机 */
#define THAISEN_POWERCONTROL_CTR_BYTE_MASK                 0xEF                /* 控制字节掩码 */

#define THAISEN_POWERCONTROLMESSAGE_TIMEOUT_QUERY_CURRENT         0x10         /* 查询电流未回复次数(超时处理) */
#define THAISEN_POWERCONTROLMESSAGE_TIMEOUT_QUERY_VOLTAGE         0x10         /* 查询电压未回复次数(超时处理) */
#define THAISEN_POWERCONTROLE_OFFLINE_COUNT                       0x30         /* 离线检测次数(大概60s)：用于电流调整 */
#define THAISEN_POWERCONTROL_OFFLINE_DETECT_COUNT                 0x08         /* 离线检测次数:用于恢复后重新开机 */

#define THAISEN_POWERCONTROL_CMD_READ_RES                         0x02         /* 指令：读取电压*/
#define THAISEN_POWERCONTROL_CMD_READ_STATE                       0x22         /* 指令：读取状态 ，不用发指令读取，状态信息一直在循环上报*/
#define THAISEN_POWERCONTROL_CMD_READ_DATA                        0x23         /* 指令：读取状态 ，不用发指令读取，状态信息一直在循环上报*/
#define THAISEN_POWERCONTROL_CMD_READ_STATE1                      0x24         /* 指令：读取状态 ，不用发指令读取，状态信息一直在循环上报*/
#define THAISEN_POWERCONTROL_CMD_READ_HEARTBEAT                   0x41         /* 指令：读取心跳一直在循环上报*/

can_msg_buf thaisen_powercontrol_volt_curr_id[THAISEN_GUN_NUM];                //设置电压电流
can_msg_buf thaisen_powercontrol_open_close_id[THAISEN_GUN_NUM];               //打开或者关闭
can_msg_buf thaisen_powercontrol_read_status_id[THAISEN_GUN_NUM];              //读取状态
can_msg_buf thaisen_powercontrol_heartbeat_id[THAISEN_GUN_NUM];                //心跳
can_msg_buf thaisen_powercontrol_set_state_id[THAISEN_GUN_NUM];                //上电后周期性发送遥信遥侧数据帧,发送枪连接，急停，门禁功率控制模块
can_msg_buf thaisen_powercontrol_set_data_id[THAISEN_GUN_NUM];                 //主控板发送电压电流等数据。

thasienPowerControlSetStruct thasienModuleSetPPOWERCONTROL;//模块类型，模块组数，模块组内个数

#pragma pack(1)//将结构体或联合体的对齐边界设置为1字节

struct powercontrolboard_id//CANID格式
{
    uint32_t source_addr : 8;
    uint32_t dest_addr : 8;
    uint32_t cmd : 8;
    uint32_t dp : 1;
    uint32_t r : 1;
    uint32_t prority : 3;
    uint32_t reserve : 3;
};

typedef struct thaisenModuleInfoStruct
{

    uint32_t volt[THAISEN_GUN_NUM];                        //组输出总电压,字节大小影响不是很大
    uint32_t curr;                        //组输出总电流
    uint8_t  powercontrol_openstate;      //打开关闭状态
    uint8_t  module_offline_count;        //离线次数
    thaisenModuleVoltCurrStruct VoltCurr; //电压电流和地址
    uint8_t  teminal_addr;                //终端地址
    struct
    {
    uint8_t    total_module_groups;       //功率模块总组数
    uint16_t   total_power;       //功率模块总功率
    uint8_t    idle_module_groups;      //空闲模块组数
    uint16_t   idle_total_power;  //空闲模块总功率
    }powercontroldata;


    struct
    {
        uint8_t voltage : 2;              /* 电压 */
        uint8_t current : 2;              /* 电流 */
    }SendCnt;                             /* 发送一次读电压电流报文就+1 */

    union
    {
        struct
        {


             uint32_t Retain5                : 18;        /* 45：预留5*/
             uint32_t OvertimeFault          : 1;         /* 44：充电机柜遥控遥信报文超时故障*/
             uint32_t OvertempFault          : 1;         /* 43：充电机柜过温故障*/
             uint32_t OnOffModuleFaulte      : 1;         /* 42：充电机柜开关模块故障*/
             uint32_t ModuleFaulte           : 1;         /* 41：充电机柜模块故障*/
             uint32_t InputphaselossFault    : 1;         /* 40：充电机柜输入缺相故障*/
             uint32_t InputUnderVolt         : 1;         /* 39：充电机柜输入欠压故障*/
             uint32_t InputOverVolt          : 1;         /* 38：充电机柜输入过压故障*/
             uint32_t GateFault              : 1;         /* 37：充电机柜门禁故障*/
             uint32_t HeatFault              : 1;         /* 36：充电机柜加热部件故障*/
             uint32_t FanFault               : 1;         /* 35：充电机柜风机故障*/
             uint32_t SmokeFault             : 1;         /* 34：充电机柜烟感故障*/
             uint32_t AcFault                : 1;         /* 33：充电机柜交流接触器故障*/
             uint32_t BreakerFault           : 1;         /* 32：充电机柜断路器故障*/
             uint32_t Retain4                : 4;         /* 28：预留*/
             uint32_t HeatingState           : 1;         /* 27：充电机加热部件状态*/
             uint32_t Retain3                : 1;         /* 26：预留*/
             uint32_t FanState               : 1;         /* 25：充电机柜风机状态*/
             uint32_t Retain2                : 7;         /* 18：预留*/
             uint32_t AcState                : 1;         /* 17：充电机柜交流接触器状态 */
             uint32_t BreakerState           : 1;         /* 16：充电机柜断路器状态*/
             uint32_t Retain1                : 5;         /* 11：预留1 */
             uint32_t WaitPowerOutput        : 1;         /* 10：等待功率输出 */
             uint32_t AllowPowerOutput       : 1;         /* 9：允许功率输出 */
             uint32_t BanPowerOutput         : 1;         /* 8：禁用功率输出*/
             uint32_t Retain                 : 3;         /* 5：预留 */
             uint32_t GroupingMode           : 1;         /* 4：分组模式 */
             uint32_t FaultState             : 1;         /* 3：故障状态 */
             uint32_t AlarmState             : 1;         /* 2：告警状态 */
             uint32_t workstate              : 2;         /* 0状态 */
        }bit;
        uint64_t fault_code;
    }state_flag;
    union
      {
          struct
          {
               uint32_t RetainFault          : 12;        /*20：预留故障（个别充电模块）*/
               uint32_t ReliefFault          : 1;         /*19：泄放故障（个别充电模块）*/
               uint32_t CorrespondenceFault  : 1;         /*18：模块通信故障（个别充电模块）*/
               uint32_t InputPhaseLossFault  : 1;         /*17：输入缺相故障（个别充电模块）*/
               uint32_t InputUnderVoltFault  : 1;         /*16：输入欠压故障（个别充电模块） */
               uint32_t InputOverVoltFault   : 1;         /*15：输入过压故障 （个别充电模块）*/
               uint32_t OutputUnderVoltFault : 1;         /*14：输出欠压故障（个别充电模块）*/
               uint32_t OutputOverVoltFault  : 1;         /*13：输出过压故障（个别充电模块）*/
               uint32_t OutputOverCurrentFault:1;         /*12：输出过流故障（个别充电模块）*/
               uint32_t OuputshortFault      : 1;         /*11：输出短路故障（个别充电模块）*/
               uint32_t AcInputFault         : 1;         /*10 交流输入故障（个别充电模块）*/
               uint32_t OverTempFault        : 1;         /*9：过温故障（个别充电模块） */
               uint32_t FanFault             : 1;         /*8：风机故障（个别充电模块）*/
               uint32_t OverTempAlarm        : 1;         /*7：过温告警 */
               uint32_t OnOffModuleAlarm     : 1;         /*6：开关模块告警 */
               uint32_t InputPhaseLossAlarm  : 1;         /*5：输入缺相告警*/
               uint32_t InputUnderVoltAlarm  : 1;         /*4：输入欠压告警 */
               uint32_t InputOverVoltAlarm   : 1;         /*3：输入过压告警 */
               uint32_t HeatAlarm            : 1;         /*2：加热模块告警 */
               uint32_t FanAlarm             : 1;         /*1：风机告警*/
               uint32_t ProtectLightAlarm    : 1;         /*0：防雷告警 */

          }bit1;
          uint32_t fault_code1;
      }state_flag1;

}thaisenPowerControl_InfoSt;

static int teminal_addr;
typedef struct
{
    uint8_t delay_count  : 6;
    uint8_t online_state : 1;
    uint8_t resume_flag  : 1;
}thaisenPowerControlOnlineState_t;


#pragma pack()//空是按照几个字节对齐

thaisenPowerControl_InfoSt thaisenPowerControl_Info = {0};//初始化结构体所有元素都是0
static thaisenModuleGeneralFaultStruct PowerControlState;//通用故障
static thaisenPowerControlOnlineState_t thaisenPowerControlOnlineState;
static uint16_t set_curr_last[THAISEN_GUN_NUM];
static uint16_t addrA;
static uint16_t addrB;

uint32_t thaisen_get_PowerControl_id(uint8_t Priority,uint8_t Retain,uint8_t Datapage,uint8_t PF,uint8_t desAddress,uint8_t sorAddress)
{
    uint32_t id;
    id = ((uint32_t)Priority << 26) | ((uint32_t)Retain << 25) | ((uint32_t)Datapage << 24) | ((uint16_t)PF << 16) | ((uint16_t)desAddress << 8)|sorAddress;
    return id;
}//组标识符

/*初始化AB枪的标识符，源地址*/
void thaisen_PowerControl_init(struct thasienPowerControlSetStruct *powerS)//gunNumber枪的个数，源地址屏幕设置A枪地址，B枪地址在A上面加1（加i）
{
    if(NULL != powerS)//如果类里面的数据不为空
        memcpy(&thasienModuleSetPPOWERCONTROL,powerS,sizeof(struct thasienPowerControlSetStruct));//就把数据传给thasienModuleSetGW
    else return ;

    for(int i = 0; i < THAISEN_GUN_NUM;i++)
    {
        if(i==0)
        {
            teminal_addr=powerS->teminal_addrA;
            rt_kprintf("11--teminal_addrA-(%d)",powerS->teminal_addrA);
        }
        else
        {
            teminal_addr=powerS->teminal_addrB;
            rt_kprintf("22--teminal_addrB-(%d)",powerS->teminal_addrB);
        }
        thaisen_powercontrol_open_close_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x01,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));
        thaisen_powercontrol_open_close_id[i].length = 8;
        thaisen_powercontrol_open_close_id[i].priority = 6;//主控板开关机指令,PF0X01

        thaisen_powercontrol_volt_curr_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x01,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));//给设置电压电流标识符
        thaisen_powercontrol_volt_curr_id[i].length = 8;
        thaisen_powercontrol_volt_curr_id[i].priority = 6;//设置电压电流,PF0X01

        thaisen_powercontrol_set_state_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x20,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));//给设置电压电流标识符
        thaisen_powercontrol_set_state_id[i].length = 8;
        thaisen_powercontrol_set_state_id[i].priority = 6;//主控板发送枪连接，急停，水浸状态,PF0X20

        thaisen_powercontrol_set_data_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x21,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));//给设置电压电流标识符
        thaisen_powercontrol_set_data_id[i].length = 8;
        thaisen_powercontrol_set_data_id[i].priority = 6;//主控板发送电压电流等数据,PF0X21

        thaisen_powercontrol_heartbeat_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x40,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));
        thaisen_powercontrol_heartbeat_id[i].length = 8;
        thaisen_powercontrol_heartbeat_id[i].priority = 6;//心跳帧
        memset(thaisen_powercontrol_heartbeat_id[i].data, 0x00, sizeof(thaisen_powercontrol_heartbeat_id[i].data));//心跳帧数据位是0，PF0X40

        thaisen_powercontrol_set_data_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x21,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));
        thaisen_powercontrol_set_data_id[i].length = 8;
        thaisen_powercontrol_set_data_id[i].priority = 6;
        memset(thaisen_powercontrol_set_data_id[i].data,0,sizeof(thaisen_powercontrol_set_data_id[i].data));

        thaisen_powercontrol_set_state_id[i].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x20,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr));
        thaisen_powercontrol_set_state_id[i].length = 8;
        thaisen_powercontrol_set_state_id[i].priority = 6;
        memset(thaisen_powercontrol_set_state_id[i].data,0,sizeof(thaisen_powercontrol_set_state_id[i].data));

        thaisenPowerControl_Info.state_flag.bit.workstate = 0x00;   /*功率控制板默认状态为关机 */

        set_curr_last[i] = 0;//上一次设置的电流

    }

    thaisenPowerControlOnlineState.resume_flag = 1;
    thaisenPowerControlOnlineState.delay_count = 0;
    thaisenPowerControlOnlineState.online_state = 1;//初始化功率控制板在线状态

    addrA=powerS->teminal_addrA;
    addrB=powerS->teminal_addrB;

}

void thaisen_set_powercontrol_open_close(uint8_t sta,uint8_t gunNum, uint8_t ChargeState, uint16_t BatVolt)//按照枪打开或者关闭功率控制模块，gunNum分别是0或1
{
#if 1
    if(gunNum > THAISEN_GUN_NUM)//暂定最多两把枪
    {
     return ;
    }

        if(sta){
            thaisenPowerControl_Info.powercontrol_openstate = 0;//sta=1,count对应的模块设置为0，英飞源模块1是关闭，0是打开
        }
        else{
            thaisenPowerControl_Info.powercontrol_openstate = 1;
        }

        thaisen_powercontrol_open_close_id[gunNum].data[0] = sta;
        thaisen_powercontrol_open_close_id[gunNum].data[0] |= thaisen_powercontrol_volt_curr_id[gunNum].data[0];
        thaisen_powercontrol_open_close_id[gunNum].data[4] = thaisen_powercontrol_volt_curr_id[gunNum].data[4];
        thaisen_powercontrol_open_close_id[gunNum].data[5] = thaisen_powercontrol_volt_curr_id[gunNum].data[5];//设置输出电流

        if(sta == 0)
        {//开机
            thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWER_CONTROL_CHARGE_RELAY;//充电回路主接触器状态闭合
            if(ChargeState <= thaisenChargInsult){
                uint16_t set_curr = thaisen_powercontrol_volt_curr_id[gunNum].data[5];
                set_curr <<= 8;
                set_curr |= thaisen_powercontrol_volt_curr_id[gunNum].data[4];
                if(set_curr < 300){
                    set_curr = 300;
                    thaisen_powercontrol_open_close_id[gunNum].data[4] = (uint8_t)set_curr;
                    thaisen_powercontrol_open_close_id[gunNum].data[5] = (uint8_t)(set_curr >>8);
                }

                thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_OPEN_FAST;//此时快速开机
            }else if(ChargeState <= thaisenChargCROAA){//如果状态是thaisenChargCROAA
                thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_SOFT_START;//此时软起开机
            }else if(ChargeState == thaisenChargCCS){//如果状态是thaisenChargCCS
                uint8_t count = 0;
//                rt_kprintf("%d*resume_flag",thaisenPowerControlOnlineState.resume_flag);

                for( count = 0; count < THAISEN_GUN_NUM; count++){
                    if(thaisenPowerControlOnlineState.resume_flag == 0x01){
                        thaisenPowerControlOnlineState.delay_count++;//延时计数加1
                        if(thaisenPowerControlOnlineState.delay_count <= 20){  //如果delay_count小于20，20*50=1000ms /* 先将电流调整至最小值 */
                            uint16_t set_curr_min = set_curr_last[gunNum];//最小电流等于上一次设置的电流
                            thaisen_powercontrol_open_close_id[gunNum].data[4] = (uint8_t)set_curr_min;
                            thaisen_powercontrol_open_close_id[gunNum].data[5] = (uint8_t)(set_curr_min >> 8);//设置电流

                            thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_POWER_RELAY;//功率分配回路接触器状态闭合
                            thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_PARA_MODIFY;//开启参数修改
                            break;
                        }else if(thaisenPowerControlOnlineState.delay_count <= 40){  //如果大于1s小于2s，40*50=2000ms/* 软起动 */
                            uint16_t set_curr_min = set_curr_last[gunNum] ;//最小电流等于上一次设置的电流
                            thaisen_powercontrol_open_close_id[gunNum].data[4] = (uint8_t)set_curr_min;
                            thaisen_powercontrol_open_close_id[gunNum].data[5] = (uint8_t)(set_curr_min >> 8);//设置电流

                            thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_SOFT_START;/* 软起动 */
                            break;
                        }else{
                            thaisenPowerControlOnlineState.resume_flag = 0;//若超过了2s
                            thaisenPowerControlOnlineState.delay_count = 0;//延时计数置0

                        }
                    }else{
                        thaisenPowerControlOnlineState.delay_count = 0;//若标志位不是1那么delay_count置0
                    }
                }
                if(count == THAISEN_GUN_NUM){//若模块数量等于一组模块数（遍历）完成
                    thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_POWER_RELAY;//功率分配回路接触器状态闭合
                    thaisen_powercontrol_open_close_id[gunNum].data[0] |= THAISEN_POWERCONTROL_PARA_MODIFY;//开启参数修改

                }

            }else{
                return;
            }
        }else{

            thaisen_powercontrol_open_close_id[gunNum].data[0] = THAISEN_POWERCONTROL_STOP_CHARGE;//如果sta！=0 1+1=2 停止充电
        }

        thaisen_powercontrol_open_close_id[gunNum].data[2] = thaisen_powercontrol_volt_curr_id[gunNum].data[2];
        thaisen_powercontrol_open_close_id[gunNum].data[3] = thaisen_powercontrol_volt_curr_id[gunNum].data[3];
        thaisen_powercontrol_open_close_id[gunNum].data[6] = (uint8_t)BatVolt;
        thaisen_powercontrol_open_close_id[gunNum].data[7] = (uint8_t)(BatVolt >>8);

    rt_mq_send(&thaisenModuleMq, &thaisen_powercontrol_open_close_id[gunNum], sizeof(can_msg_buf));//把模块打开或者关闭的状态发送给这个消息队列thaisenModuleMq
#endif
}


void thaisen_set_powercontrol_volt_curr(uint32_t volt,uint32_t curr,uint8_t gunNum, uint8_t ChargeState)//设置功率控制模块总电压，电流输入已经乘100了，电压输入已经乘10了
{
    static uint32_t period_tick[THAISEN_GUN_NUM];
    if(gunNum > THAISEN_GUN_NUM)//暂定最多两把枪
    {
     return ;
    }
//    if((ChargeState <= thaisenChargCROAA) || (curr < set_curr_last[gunNum])){
//            set_curr_last[gunNum] = curr;
//            period_tick[gunNum] = rt_tick_get();
//        }else{
//            if(curr == set_curr_last[gunNum]){
//                period_tick[gunNum] = rt_tick_get();
//            }
//            if(period_tick[gunNum] > rt_tick_get()){
//                period_tick[gunNum] = rt_tick_get();
//            }
//            if((rt_tick_get() - period_tick[gunNum]) >= 10000){
//                period_tick[gunNum] = rt_tick_get();
//                set_curr_last[gunNum] = curr;
//            }
//        }

//    thaisen_powercontrol_volt_curr_id[gunNum].CANID = thaisen_get_PowerControl_id(0x06,0x00,0x00,0x01,0x10,(THAISEN_TERMINAL_ADDRESS_TEMINAL+teminal_addr+gunNum));//没关系设的本来就是每个模块的电压电流
//    thaisen_powercontrol_volt_curr_id[gunNum].length = 8;
//    thaisen_powercontrol_volt_curr_id[gunNum].priority = 6;
//    memset(thaisen_powercontrol_volt_curr_id[gunNum].data,0,sizeof(thaisen_powercontrol_volt_curr_id[gunNum].data));

    thaisen_powercontrol_volt_curr_id[gunNum].data[2] = (uint8_t)(volt >> 0);
    thaisen_powercontrol_volt_curr_id[gunNum].data[3] = (uint8_t)(volt >> 8);//易能模块采用低字节优先发送的策略，把数据的地位转为date的高位

    thaisen_powercontrol_volt_curr_id[gunNum].data[4] = (uint8_t)(curr >> 0);
    thaisen_powercontrol_volt_curr_id[gunNum].data[5] = (uint8_t)(curr >> 8);
    if(volt <= 5000)
    {
        thaisen_powercontrol_volt_curr_id[gunNum].data[0] = 0;
    }
    else if(volt > 5000)
    {
        thaisen_powercontrol_volt_curr_id[gunNum].data[0] = THAISEN_POWERCONTROL_VOLT_RANGE;
    }
    set_curr_last[gunNum] = curr;
}//把电压电流发给消息队列

void thaisen_set_terminal_status(uint8_t gunNum,uint8_t connectstatus,uint8_t scram,uint8_t gate,uint8_t water)//主控板发送枪连接，急停，水浸状态,PF0X20
{
    if(gunNum > THAISEN_GUN_NUM)//暂定最多两把枪
    {
     return ;
    }
    uint32_t time = mw_get_current_timestamp();
    thaisen_powercontrol_set_state_id[gunNum].data[0] = (uint8_t)connectstatus;//未插枪12V，插枪6V,CC1闭合4V
    thaisen_powercontrol_set_state_id[gunNum].data[1] = (uint8_t)scram;
    thaisen_powercontrol_set_state_id[gunNum].data[2] = (uint8_t)gate;
    thaisen_powercontrol_set_state_id[gunNum].data[3] = (uint8_t)water;//采用低字节优先发送的策略，把数据的地位转为date的高位
    thaisen_powercontrol_set_state_id[gunNum].data[4] = (uint8_t)(time >> 0);
    thaisen_powercontrol_set_state_id[gunNum].data[5] = (uint8_t)(time >> 8);
    thaisen_powercontrol_set_state_id[gunNum].data[6] = (uint8_t)(time >> 16);
    thaisen_powercontrol_set_state_id[gunNum].data[7] = (uint8_t)(time >> 24);

    rt_mq_send(&thaisenModuleMq, &thaisen_powercontrol_set_state_id[gunNum], sizeof(can_msg_buf));
}//把电压电流发给消息队列

void thaisen_set_terminal_data(uint8_t gunNum,uint16_t outputvolt,uint16_t outputcurr,uint16_t outputpower,uint16_t chargetime)//主控板发送输出电压，输出电流，输出功率，充电时长,PF0X21
{
    if(gunNum > THAISEN_GUN_NUM)//暂定最多两把枪
    {
     return ;
    }

    thaisen_powercontrol_set_data_id[gunNum].data[0] = (uint8_t)(outputvolt >> 0);
    thaisen_powercontrol_set_data_id[gunNum].data[1] = (uint8_t)(outputvolt >> 8);

    thaisen_powercontrol_set_data_id[gunNum].data[2] = (uint8_t)(outputcurr >> 0);
    thaisen_powercontrol_set_data_id[gunNum].data[3] = (uint8_t)(outputcurr >> 8);

    thaisen_powercontrol_set_data_id[gunNum].data[4] = (uint8_t)(outputpower >> 0);
    thaisen_powercontrol_set_data_id[gunNum].data[5] = (uint8_t)(outputpower >> 8);

    thaisen_powercontrol_set_data_id[gunNum].data[6] = (uint8_t)(chargetime >> 0);
    thaisen_powercontrol_set_data_id[gunNum].data[7] = (uint8_t)(chargetime >> 8);

    rt_mq_send(&thaisenModuleMq, &thaisen_powercontrol_set_data_id[gunNum], sizeof(can_msg_buf));
}//把电压电流发给消息队列

void thaisen_send_heartbeat_powercontrol_broadcast(uint8_t groupNum)
{
    rt_mq_send(&thaisenModuleMq, &thaisen_powercontrol_heartbeat_id[groupNum], sizeof(can_msg_buf));

}

void thaisen_read_powercontrol_status_group(uint8_t gunNum)//离线查询功率控制模块
{
//            rt_kprintf("--1_--%d--\r\n",PowerControlState.fault.ModuleFault);

    if(gunNum >= THAISEN_GUN_NUM)
    {
        return;
    }

    if(thaisenPowerControl_Info.module_offline_count < 0xFF)//离线查询次数小于225，在process函数中收到数据就会置零
    {
        thaisenPowerControl_Info.module_offline_count++;
    }
/******************************************A枪读取报文，B枪读取报文*************************************************/


            if(thaisenPowerControl_Info.SendCnt.current >= THAISEN_POWERCONTROLMESSAGE_TIMEOUT_QUERY_CURRENT){
                thaisenPowerControl_Info.curr = 0;//如果读电流报文超过16次，电流设为零//这里面已经不递增了
            }
            if(thaisenPowerControl_Info.SendCnt.voltage >= THAISEN_POWERCONTROLMESSAGE_TIMEOUT_QUERY_VOLTAGE){
                thaisenPowerControl_Info.volt[gunNum] = 0;
            }

            if(thaisenPowerControl_Info.module_offline_count >= THAISEN_POWERCONTROL_OFFLINE_DETECT_COUNT){//离线次数大于8次
                thaisenPowerControlOnlineState.online_state = 0;//离线状态判为0

            }else{
                if(thaisenPowerControlOnlineState.online_state == 0){//若离线状态小于8次，状态为离线，离线状态变为在线
                   thaisenPowerControlOnlineState.resume_flag = 1;//恢复标志位置为在线
                   thaisenPowerControlOnlineState.delay_count = 0;
                   thaisenPowerControlOnlineState.online_state = 1;
            }

            if(thaisenPowerControl_Info.powercontrol_openstate == 0)//如果模块的状态是关闭的该枪的地址设为count，电流设为零
            {
                thaisenPowerControl_Info.volt[gunNum] = 0;

                thaisenPowerControl_Info.curr = 0;
            }
        }

}

void thaisen_set_powercontrol_address(void)
{
}

void thaisen_set_powercontrol_group(void)
{

}

void thaisen_read_powercontrol_volt_curr_group(uint8_t groupNum)
{

}


uint32_t thaisen_get_powercontrol_volt(uint8_t gunNum)//获取模块的电压
{
    if(gunNum >= THAISEN_MODULE_GROUP_NUM)
    {
        return 0;
    }
    return thaisenPowerControl_Info.volt[gunNum];
}

void* thaisen_get_powercontrol_voltage_current(uint8_t groupNum, uint8_t Sequence)//第几组第几个模块
{

}

void*  thaisen_getpowercontrol_state(uint8_t gunNum, uint8_t Sequence)
{
//    rt_kprintf("_____%d___module_offline_count__",thaisenPowerControl_Info.module_offline_count);
    if(gunNum >= THAISEN_GUN_NUM){
        return 0;
    }

    PowerControlState.fault.InputOverVolt = thaisenPowerControl_Info.state_flag1.bit1.InputOverVoltFault;
    PowerControlState.fault.InputUnderVolt = thaisenPowerControl_Info.state_flag1.bit1.InputUnderVoltFault;
    PowerControlState.fault.OutputOverVolt = thaisenPowerControl_Info.state_flag1.bit1.OutputOverVoltFault;
    PowerControlState.fault.OutputUnderVolt = thaisenPowerControl_Info.state_flag1.bit1.OutputUnderVoltFault;
    PowerControlState.warn.Fan = thaisenPowerControl_Info.state_flag.bit.FanFault;
    PowerControlState.fault.OverTemp = thaisenPowerControl_Info.state_flag.bit.OvertempFault;
    PowerControlState.fault.OverCurr = thaisenPowerControl_Info.state_flag1.bit1.OutputOverCurrentFault;
    PowerControlState.state.BootState = 0x00;
    if(thaisenPowerControl_Info.state_flag.bit.workstate == 0x02){
        PowerControlState.state.BootState = 0x01;
    }
    PowerControlState.fault.ModuleFault = thaisenPowerControl_Info.state_flag.bit.ModuleFaulte;//模块故障要用到，没有的话就不赋值
//    rt_kprintf("--3_--%d-OvertempFault-\r\n",thaisenPowerControl_Info.state_flag.bit.OvertempFault);
    if(thaisenPowerControl_Info.module_offline_count >= THAISEN_POWERCONTROLE_OFFLINE_COUNT){
    PowerControlState.fault.ModuleFault = 1;
//        rt_kprintf("--2_--%d--\r\n",PowerControlState.fault.ModuleFault);
    }
    PowerControlState.temperature = 0;

    return &PowerControlState;
}


void thaisen_powercontrol_process(void)
{
    can_msg_buf rev_powercontrol_id;//接收模块消息的ID
    struct powercontrolboard_id powercontrol_id;

    rev_powercontrol_id = *thaisen_get_can_charg_module_dat();//接收的充电模块CAN数据给rev_powercontrol_id
    memcpy(&powercontrol_id, &rev_powercontrol_id.CANID, sizeof(powercontrol_id));

//      if(rev_powercontrol_id.CANID == 0)
        //如果接受数据的目标地址不在0x30~0x4F之间
    if((powercontrol_id.dest_addr>0x4F)||(powercontrol_id.dest_addr<0x30))//如果接受数据的目标地址不在0x30~0x4F之间
        {
//        rt_kprintf("%x&&&&",rev_powercontrol_id.CANID);
        memset(thaisen_get_can_charg_module_dat(),0,sizeof(can_msg_buf));//充电模块CAN数据置零
        return;
    }
//    rt_kprintf("%x/n",*thaisen_get_can_charg_module_dat());
    uint16_t dest_addr;
    for(int i = 0; i < THAISEN_GUN_NUM;i++)
    {
        if(i==0)
        {
            dest_addr=addrA;

        }
        else
        {
            dest_addr=addrB;
        }
    if((powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_RES)&&(powercontrol_id.dest_addr==THAISEN_TERMINAL_ADDRESS_TEMINAL+dest_addr))
//    if(powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_RES)
    {
        thaisenPowerControl_Info.volt[i] = rev_powercontrol_id.data[3];
        thaisenPowerControl_Info.volt[i]<<=8;
        thaisenPowerControl_Info.volt[i] |= rev_powercontrol_id.data[2];
        if(thaisen_powercontrol_open_close_id[0].data[0]==THAISEN_POWERCONTROL_STOP_CHARGE)
        {
            thaisenPowerControl_Info.volt[THAISEN_GUN_NUM] = 0;
        }
    }
//    rt_kprintf("*******%d*******/n",thaisenPowerControl_Info.volt[i]);
        }
    if(powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_STATE)//命令是读取模块的状态,把此时的模块状态给state_flag，判断有什么故障
    {
        thaisen_clear_SentErrCnt(THAISEN_GUN_A);//清除指定枪的发送错误计数
        thaisen_clear_SentErrCnt(THAISEN_GUN_B);

        uint32_t state_flag = rev_powercontrol_id.data[0];

        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[1];//把状态位的地址给state_flag，state_flag给thaisenModuleInfo_gw_thaisenModuleInfo_gw_
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[3];
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[2];
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[7];
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[6];
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[5];
        state_flag <<= 8;
        state_flag |= rev_powercontrol_id.data[4];
        for (int i = 0; i < 8; i++) {
//        rt_kprintf("%x ", rev_powercontrol_id.data[2]);
//        rt_kprintf("/n");
        }
            thaisenPowerControl_Info.state_flag.fault_code = state_flag;
            thaisenPowerControl_Info.module_offline_count = 0;
    }
//    rt_kprintf("%x ", thaisenPowerControl_Info.state_flag.bit.workstate);
//    rt_kprintf("%x ", thaisenPowerControl_Info.state_flag.bit.AlarmState);
//    rt_kprintf("%x ", thaisenPowerControl_Info.state_flag.bit.FaultState);
//    rt_kprintf("%x ", thaisenPowerControl_Info.state_flag.bit.HeatingState);
//    rt_kprintf("%x ", thaisenPowerControl_Info.state_flag.bit.AcFault);

    if(powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_DATA)//命令是读取模块的状态,把此时的模块状态给state_flag，判断有什么故障
    {
        thaisenPowerControl_Info.powercontroldata.total_module_groups = rev_powercontrol_id.data[0];
        thaisenPowerControl_Info.powercontroldata.total_power = (rev_powercontrol_id.data[2] << 8) | rev_powercontrol_id.data[1];
        thaisenPowerControl_Info.powercontroldata.idle_module_groups = rev_powercontrol_id.data[3];
        thaisenPowerControl_Info.powercontroldata.idle_total_power = (rev_powercontrol_id.data[5] << 8) | rev_powercontrol_id.data[5];
    }

    if(powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_STATE1)//命令是读取模块的状态,把此时的模块状态给state_flag，判断有什么故障
    {


        uint32_t state_flag1 = rev_powercontrol_id.data[3];

        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[2];//把状态位的地址给state_flag，state_flag给thaisenModuleInfo_gw_thaisenModuleInfo_gw_
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[1];
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[0];
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[7];
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[6];
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[5];
        state_flag1 <<= 8;
        state_flag1 |= rev_powercontrol_id.data[4];


            thaisenPowerControl_Info.state_flag.fault_code = state_flag1;
            thaisenPowerControl_Info.module_offline_count = 0;
    }

    if(powercontrol_id.cmd == THAISEN_POWERCONTROL_CMD_READ_HEARTBEAT)
    {
        thaisenPowerControl_Info.module_offline_count = 0;
    }
}









